"""
    @Author  ：思念 
    @File    ：3.aiohttp简单使用.py
    @Date    ：2024/12/28 18:34 
"""

import aiohttp
import asyncio


url = 'https://www.baidu.com'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
}

async def get_baidu():
    # # 1.创建请求对象
    # aiohttp_session = aiohttp.ClientSession()
    # # 2.发送请求
    # response = await aiohttp_session.get(url=url, headers=headers)
    # # 3.获取响应
    # print(response.status)
    # print(await response.text())
    # # 4.关闭请求对象
    # await aiohttp_session.close()
    # response.close()

    async with aiohttp.ClientSession() as session:
        async with session.get(url=url, headers=headers) as response:
            response = await response.text()
            print(response)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(get_baidu())